//
//  SAOverallSetting.m
//  Saas
//
//  Created by 杨荣 on 2020/4/8.
//  Copyright © 2020 bing. All rights reserved.
//

#import "SAOverallSetting.h"
#import "SANetManager.h"
#import "SACommoditySpecialFeesModel.h"

@interface SAOverallSetting ()


@property (nonatomic,copy) NSString *isShowStockSettingId;

@property (nonatomic,copy) NSString *latestSellPriceTypeSettingId;

@property (nonatomic,copy) NSString *isShowReplenishmentSettingId;

@property (nonatomic,copy) NSString *isShowReplenishmentReturnSettingId;

@property (nonatomic,copy) NSString *timeLimitReturnSettingId;

@property (nonatomic,copy) NSString *lowestSellPriceTypeSettingId;

@property (nonatomic,copy) NSString *isShowlatestRemarkSettingId;

@property (nonatomic,copy) NSString *specialExpensesArrayParentId;

@property (nonatomic,copy) NSString *isShowBarcodeSettingId;


@end

@implementation SAOverallSetting

static SAOverallSetting *overallSetting;
+ (SAOverallSetting *)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        overallSetting = [[SAOverallSetting alloc] init];
        [overallSetting initData];
    });
    return overallSetting;
}

- (void)initData {
    
    self.user = [SAUser shareInstance];
    self.printerSetting = [SAPrinterSetting shareInstance];
    self.isShowStock = YES;
    self.latestSellPriceType = 0;
    self.isShowReplenishment = NO;
    self.isShowReplenishmentReturn = NO;
    self.timeLimitReturn = 0;
    self.lowestSellPriceType = 0;
    self.isShowlatestRemark = NO;
    self.specialExpensesArray = @[];
    self.isShowBarcode = 1;
}

#pragma maark - Request
#pragma mark  获取全局设置
- (void)requestByGetOverallSettingWithSuccessBlock:(NetworkingSuccessBlock)successBlock
                                         failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [SANetManager requestWithType:(SkyHttpRequestTypeGet) urlString:SA_BASE_URL(@"/system/settings/list") parameters:parameters successBlock:^(id responseObject) {
        
        NSDictionary *dataDic = responseObject;
        
        // 打印机设置
        NSDictionary *printerSettingDic = dataDic[@"printer_setting"];
        self.printerSetting.printCopies = [printerSettingDic[@"sales_print_copies"][@"value"] integerValue];//  打印份数：0-不打印 1-打印1份 2-打印2份 3-打印3份 4打印4份
        self.printerSetting.printTemplateType = [printerSettingDic[@"receipt_print_layout"][@"value"] integerValue]; // 打印小票排版：0-尺码横排   1-一组颜色尺码打一行
        self.printerSetting.printCopiesParentId = printerSettingDic[@"sales_print_copies"][@"parentId"];
        self.printerSetting.printCopiesSettingId = printerSettingDic[@"sales_print_copies"][@"settingsId"];
        self.printerSetting.printTemplateTypeParentId = printerSettingDic[@"receipt_print_layout"][@"parentId"];
        self.printerSetting.printTemplateTypeSettingId = printerSettingDic[@"receipt_print_layout"][@"settingsId"];
        
        //  是否显示库存
        NSDictionary *isShowStockDic = dataDic[@"show_stock"];
        self.isShowStock = [isShowStockDic[@"value"] boolValue];// 0-不显示库存 1-显示库存
        self.isShowStockSettingId = isShowStockDic[@"settingsId"];
        
        // 最近拿货价类型
        NSDictionary *latestSellPriceTypeDic = dataDic[@"recent_get_price"];
        self.latestSellPriceType = [latestSellPriceTypeDic[@"value"] integerValue];//  0-不显示最近拿货价    1-只显示最近拿货价     2-取最近开单价
        self.latestSellPriceTypeSettingId = latestSellPriceTypeDic[@"settingsId"];
        
        // 是否显示补货标识
        NSDictionary *isShowReplenishmentDic = dataDic[@"replenishment_sign"];
        self.isShowReplenishment = [isShowReplenishmentDic[@"value"] boolValue];// 0-不显示补货标识 1-显示补货标识
        self.isShowReplenishmentSettingId = isShowReplenishmentDic[@"settingsId"];
        
        // 是否显示补货退货标识
        NSDictionary *isShowReplenishmentReturnDic = dataDic[@"replenishment_returns_sign"];
        self.isShowReplenishmentReturn = [isShowReplenishmentReturnDic[@"value"] boolValue];// 0-不显示补货退货标识 1-显示补货退货标识
        self.isShowReplenishmentReturnSettingId = isShowReplenishmentReturnDic[@"settingsId"];
        
        // 期限退货
        NSDictionary *timeLimitReturnDic = dataDic[@"deadline_returns"];
        self.timeLimitReturn = [timeLimitReturnDic[@"value"] integerValue];//  0-无限期    N>0-期限退货N天
        self.timeLimitReturnSettingId = timeLimitReturnDic[@"settingsId"];
        
        // 最低销售价类型
        NSDictionary *lowestSellPriceTypeDic = dataDic[@"lowest_sales_price"];
        self.lowestSellPriceType = [lowestSellPriceTypeDic[@"value"] floatValue];//  0-无限制    (0,1)-不低于固定比例     >1-不低于固定金额
        self.lowestSellPriceTypeSettingId = lowestSellPriceTypeDic[@"settingsId"];
        
        // 是否显示最近拿货的备注
        NSDictionary *isShowlatestRemarkDic = dataDic[@"recent_get_remarks"];
        self.isShowlatestRemark = [isShowlatestRemarkDic[@"value"] boolValue];// 0-不显示最近拿货的备注 1-显示最近拿货的备注
        self.isShowlatestRemarkSettingId = isShowlatestRemarkDic[@"settingsId"];
        
        // 额外费用
        NSDictionary *specialExpensesArrayDic = dataDic[@"special_cost"];
        NSArray *tempArray = specialExpensesArrayDic[@"childSettings"];
        NSMutableArray *array = [NSMutableArray array];
        for (NSDictionary *tempDic in tempArray) {
            BOOL isCustom =  [tempDic[@"code"] length] <= 0;
            BOOL isOpen = [tempDic[@"value"] boolValue];
            [array addObject:@{@"name":tempDic[@"name"],
                               @"parentId":tempDic[@"parentId"],
                               @"settingsId":tempDic[@"settingsId"],
                               @"isCustom":[NSNumber numberWithInteger:isCustom],
                               @"isOpen":[NSNumber numberWithInteger:isOpen],}];
        }
        self.specialExpensesArray = array;// 额外费用集合 {"name","parentId", "settingsId", "isCustom", "isOpen"}
        self.specialExpensesArrayParentId = specialExpensesArrayDic[@"templateId"];
        
        // 条码设置
        NSDictionary *barcodeSettingTypeDic = dataDic[@"bar_code"];
        self.isShowBarcode = [barcodeSettingTypeDic[@"value"] boolValue];// 0-关闭    1-开启
        self.isShowBarcodeSettingId = barcodeSettingTypeDic[@"settingsId"];
        
        // 标签排版
        self.labelLayout = [SACommoditySpecialFeesModel mj_objectWithKeyValues:printerSettingDic[@"label_layout"]];
        
        // 标签排版属性
        self.labelLayoutAttribute = [SACommoditySpecialFeesModel mj_objectWithKeyValues:printerSettingDic[@"label_layout_attribute"]];
        
        self.labelLayoutAttribute4030 = [SACommoditySpecialFeesModel mj_objectWithKeyValues:printerSettingDic[@"label_layout_attribute_4030"]];
        
        self.labelLayoutAttribute4060 = [SACommoditySpecialFeesModel mj_objectWithKeyValues:printerSettingDic[@"label_layout_attribute_4060"]];
        
        self.labelLayoutAttribute4080 = [SACommoditySpecialFeesModel mj_objectWithKeyValues:printerSettingDic[@"label_layout_attribute_4080"]];
        
        self.labelLayoutAttribute3020 = [SACommoditySpecialFeesModel mj_objectWithKeyValues:printerSettingDic[@"label_layout_attribute_3020"]];
        
        self.labelLayoutAttribute6040 = [SACommoditySpecialFeesModel mj_objectWithKeyValues:printerSettingDic[@"label_layout_attribute_6040"]];
        
        self.sizeValue4030 = [SACommoditySpecialFeesModel mj_objectWithKeyValues:printerSettingDic[@"size_value_4030"]];
        
        self.sizeValue4060 = [SACommoditySpecialFeesModel mj_objectWithKeyValues:printerSettingDic[@"size_value_4060"]];
        
        self.sizeValue4080 = [SACommoditySpecialFeesModel mj_objectWithKeyValues:printerSettingDic[@"size_value_4080"]];
        
        self.sizeValue3020 = [SACommoditySpecialFeesModel mj_objectWithKeyValues:printerSettingDic[@"size_value_3020"]];
        
        self.sizeValue6040 = [SACommoditySpecialFeesModel mj_objectWithKeyValues:printerSettingDic[@"size_value_6040"]];
        
        self.ticketFontSize = [SACommoditySpecialFeesModel mj_objectWithKeyValues:printerSettingDic[@"ticket_font_size"]];
        
        // 小票排版属性
        self.ticketLayoutAttribute = [SACommoditySpecialFeesModel mj_objectWithKeyValues:dataDic[@"small_ticket"][@"ticket_layout_attribute"]];
        
        // 打印尺寸
        self.printSize = [SACommoditySpecialFeesModel mj_objectWithKeyValues:dataDic[@"small_ticket"][@"receipt_print_ticket"]];
        
        self.settlementMethod = [SACommoditySpecialFeesModel mj_objectWithKeyValues:dataDic[@"settlement_method"]];
        
        self.hkSymbol = [SACommoditySpecialFeesModel mj_objectWithKeyValues:printerSettingDic[@"hk_symbol"]];
        
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

#pragma mark 更新【是否显示库存】
- (void)requestByUpdateIsShowStockWithSuccessBlock:(NetworkingSuccessBlock)successBlock
                                         failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:self.isShowStockSettingId forKey:@"settingsId"];
    [parameters setValue:[NSNumber numberWithInteger:self.isShowStock] forKey:@"value"];
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/system/settings/open") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        self.isShowStock = !self.isShowStock;
        if (failBlock) {
            failBlock(error);
        }
    }];
}
// 更新【最近拿货价】
- (void)requestByUpdateLatestSellPriceTypeSuccessBlock:(NetworkingSuccessBlock)successBlock
                                             failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:self.latestSellPriceTypeSettingId forKey:@"settingsId"];
    [parameters setValue:[NSNumber numberWithInteger:self.latestSellPriceType] forKey:@"value"];
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/system/settings/check") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}
// 更新【是否显示补货标识】
- (void)requestByUpdateIsShowReplenishmentWithSuccessBlock:(NetworkingSuccessBlock)successBlock
                                                 failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:self.isShowReplenishmentSettingId forKey:@"settingsId"];
    [parameters setValue:[NSNumber numberWithInteger:self.isShowReplenishment] forKey:@"value"];
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/system/settings/open") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        self.isShowReplenishment = !self.isShowReplenishment;
        if (failBlock) {
            failBlock(error);
        }
    }];
}
// 更新【是否显示补货退货标识】
- (void)requestByUpdateIsShowReplenishmentReturnWithSuccessBlock:(NetworkingSuccessBlock)successBlock
                                                       failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:self.isShowReplenishmentReturnSettingId forKey:@"settingsId"];
    [parameters setValue:[NSNumber numberWithInteger:self.isShowReplenishmentReturn] forKey:@"value"];
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/system/settings/open") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        self.isShowReplenishmentReturn = !self.isShowReplenishmentReturn;
        if (failBlock) {
            failBlock(error);
        }
    }];
}
// 更新【期限退货】
- (void)requestByUpdateTimeLimitReturnSuccessBlock:(NetworkingSuccessBlock)successBlock
                                         failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:self.timeLimitReturnSettingId forKey:@"settingsId"];
    [parameters setValue:[NSNumber numberWithInteger:self.timeLimitReturn] forKey:@"value"];
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/system/settings/check") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}
// 更新【最低销售价】
- (void)requestByUpdateLowestSellPriceTypeSuccessBlock:(NetworkingSuccessBlock)successBlock
                                             failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:self.lowestSellPriceTypeSettingId forKey:@"settingsId"];
    [parameters setValue:[NSString stringWithFormat:@"%.2f",self.lowestSellPriceType] forKey:@"value"];
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/system/settings/check") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}
// 更新【是否显示最近拿货的备注】
- (void)requestByUpdateIsShowlatestRemarkWithSuccessBlock:(NetworkingSuccessBlock)successBlock
                                                failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:self.isShowlatestRemarkSettingId forKey:@"settingsId"];
    [parameters setValue:[NSNumber numberWithInteger:self.isShowlatestRemark] forKey:@"value"];
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/system/settings/open") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        self.isShowlatestRemark = !self.isShowlatestRemark;
        if (failBlock) {
            failBlock(error);
        }
    }];
}
// 更新【是否显示条码】
- (void)requestByUpdateIsShowBarcodeWithSuccessBlock:(NetworkingSuccessBlock)successBlock
                                           failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:self.isShowBarcodeSettingId forKey:@"settingsId"];
    [parameters setValue:[NSNumber numberWithInteger:self.isShowBarcode] forKey:@"value"];
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/system/settings/open") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        self.isShowBarcode = !self.isShowBarcode;
        if (failBlock) {
            failBlock(error);
        }
    }];
}


//{
//    "name": "",
//    "parentId": 0,
//    "value": "",
//    "valueType": 0
//}

//self.specialExpensesArray = array;// 额外费用集合 {"name","parentId", "settingsId", "isCustom", "isOpen"}

// 添加【额外费用】
- (void)requestByAddSpecialExpense:(NSString *)specialExpense
                       uccessBlock:(NetworkingSuccessBlock)successBlock
                         failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:specialExpense forKey:@"name"];
    [parameters setValue:self.specialExpensesArrayParentId forKey:@"parentId"];
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/system/settings/addChild") parameters:parameters successBlock:^(id responseObject) {
        
        [self requestByGetOverallSettingWithSuccessBlock:^(id  _Nonnull response) {
            if (successBlock) {
                successBlock(responseObject);
            }
        } failBlock:^(NSString * _Nonnull errorMessage) {
            if (failBlock) {
                failBlock(errorMessage);
            }
        }];
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

// 更新【额外费用】
- (void)requestByUpdateSpecialExpenses:(NSArray <NSDictionary *>*)specialExpenses
                           uccessBlock:(NetworkingSuccessBlock)successBlock
                             failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:specialExpenses forKey:@"switchList"];
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/system/settings/openMany") parameters:parameters successBlock:^(id responseObject) {
        
        [self requestByGetOverallSettingWithSuccessBlock:^(id  _Nonnull response) {
            if (successBlock) {
                successBlock(responseObject);
            }
        } failBlock:^(NSString * _Nonnull errorMessage) {
            if (failBlock) {
                failBlock(errorMessage);
            }
        }];
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)requestByUpdateSettlementTypeSuccessBlock:(NetworkingSuccessBlock)successBlock
                                             failBlock:(NetworkingFailBlock)failBlock {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:self.settlementMethod.settingsId forKey:@"settingsId"];
    [parameters setValue:self.settlementMethod.value forKey:@"value"];
    [SANetManager requestWithType:(SkyHttpRequestTypePost) urlString:SA_BASE_URL(@"/system/settings/check") parameters:parameters successBlock:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failureBlock:^(NSString *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

@end

